﻿#include "MainMeau.h"
#include "ui_MainMeau.h"
#include "ui_widget.h"

MainMeau::MainMeau(SOCKET sockCli,QString qq,QString pwd,QWidget *parent) :
    QWidget(parent),
    sockCli(sockCli),
    qq(qq),
    pwd(pwd),
    ui(new Ui::MainMeau)
{
    ui->setupUi(this);
    //设置MainMeau名字
    this->setWindowTitle(u8"用户" + qq);
    ConnectThreadClass = new ConnectThread(sockCli);
    QThreadOfConnectThread = new QThread;
    ConnectThreadClass->moveToThread(QThreadOfConnectThread);//新起一个线程监听服务端
    initInterface();
    QThreadOfConnectThread->start(); //开始监听
    initMainMeau();
    qDebug()<<u8"MainMeau构造函数完毕！";
}
void MainMeau::initInterface(){
    //  客户端接收到服务端私聊信息时的处理的连接
    void(ConnectThread::*s)(Message) = &ConnectThread::noticeReceviceMessage;
    void(MainMeau::*r)(Message) = &MainMeau::showMessage;
    connect(ConnectThreadClass,s,this,r);
    //  客户端接收到服务端拉取好友列表信息时的处理的连接
    void(ConnectThread::*nrm)(Message) = &ConnectThread::noticeGetFirendList;
    void(MainMeau::*sfl)(Message) = &MainMeau::showFirendList;
    connect(ConnectThreadClass,nrm,this,sfl);
    //  客户端接收到服务端更新新朋友信息时的处理的连接
    void(ConnectThread::*unf)(Message) = &ConnectThread::updateNewFriend;
    void(MainMeau::*unfl)(Message) = &MainMeau::updateNewFriendList;
    connect(ConnectThreadClass,unf,this,unfl);
    //  客户端接收到服务端添加好友成功信息时的处理的连接
    void(ConnectThread::*afs)(Message) = &ConnectThread::AddFriendSucessfully;
    void(MainMeau::*afss)(Message) = &MainMeau::addFriendSucessfully;
    connect(ConnectThreadClass,afs,this,afss);
    //  客户端接收到服务端添加好友请求信息时的处理的连接
    void(ConnectThread::*raf)(Message) = &ConnectThread::requestAddFriend;
    void(MainMeau::*rafs)(Message) = &MainMeau::requestAddFriend;
    connect(ConnectThreadClass,raf,this,rafs);
    //  客户端开启监听的连接
    connect(QThreadOfConnectThread, SIGNAL(started()),ConnectThreadClass, SLOT(connectWithServer()));
}
void MainMeau::initMainMeau(){
    //向服务器申请拉取好友列表
    qDebug()<<u8"向服务器申请拉取好友列表";
    m.messageType = 4;
    strcpy(m.sender,qq.toStdString().c_str());
    qDebug()<<u8"向服务器申请拉取好友列表,发送了:"<<send(sockCli,(char*)&m,sizeof(m),0);
    //向服务器申请拉取新朋友信息
    qDebug()<<u8"向服务器申请拉取新朋友信息";
    Sleep(50);
    m.messageType = 7;
    strcpy(m.sender,qq.toStdString().c_str());
    qDebug()<<u8"向服务器申请拉取新朋友信息,发送了:"<<send(sockCli,(char*)&m,sizeof(m),0);
}
/*
 * 显示好友列表
*/
void MainMeau::showFirendList(Message m){
    qDebug()<<u8"MainMeau::showFirendList";
    for(int i = 0;i<MAX_FIREND_NUM;i++){
        std::string fid = m.f[i].firendId;
        fid = fid.substr(0,4);//好友id
        if(fid == "0000") break;
        //增加朋友
        friends.insert(pair<string,Firend>(fid,m.f[i]));
        //增加窗口
        Widget* w = new Widget();
        w->sockCli = sockCli;
        w->sender = qq;
        w->recevicer = fid.c_str();
        w->f = &friends.find(fid)->second;
        friendsOfWidget.insert(pair<string,Widget*>(fid,w));
        //增加按钮
        QPushButton* btn = new QPushButton(this);//增加按钮
        btn->setText(u8"好友" + QString::fromStdString(fid));//增加好友名称
        btn->setFixedSize(200,50);
        friendsOfBtn.insert(pair<string,QPushButton*>(fid,btn));
        ui->Friendlist->addWidget(btn);//增加到好友列表页面
        //加槽函数   点击按钮打开聊天窗
        connect(btn,&QPushButton::clicked,this,[=](){
            qDebug()<<u8"按下按钮";
            btn->setText(u8"好友" + QString::fromStdString(fid));
            Widget* thisWidget = friendsOfWidget.find(fid)->second;
            if(thisWidget->isOpen == false){
                thisWidget->setWindowTitle(u8"好友" + QString::fromStdString(fid));
                thisWidget->ui->ShowText->setText(thisWidget->f->chatContent);
                thisWidget->show();
                thisWidget->isOpen = true;
            }
        });
    }
    this->show();
}
/*
 * 更新新朋友请求信息
*/
void MainMeau::updateNewFriendList(Message m){
    qDebug()<<"updateNewFriendList";
    newFriend.clear(); //清空所有
    for(int i = 0;i<MAX_FIREND_NUM;i++){
        string qq = m.f[i].firendId;
        if(qq.substr(0,4) == "0000") break;
        string remarks = m.f[i].chatContent;
        newFriend.insert(pair<string,string>(qq.substr(0,4),remarks));
    }
}
/*
 * 收到一个好友请求
*/
void MainMeau::requestAddFriend(Message m){
    string newFriendId = m.sender;
    string remarks = m.messageContent;
    newFriend.insert(pair<string,string>(newFriendId.substr(0,4),remarks));
}
/*
 * 收到私聊消息
*/
void MainMeau::showMessage(Message m){
    qDebug()<<"showMessage ";
    qDebug()<<m.f[0].chatContent;
    //解析发送者
    std::string sender = m.sender;
    sender = sender.substr(0,4);
    auto it = friends.find(sender);
    strcpy(it->second.chatContent,m.f[0].chatContent);//更新
    Widget* thisWidget = friendsOfWidget.find(sender)->second;
    if(thisWidget->isOpen){
        thisWidget->ui->ShowText->setText(thisWidget->f->chatContent);
    }else{
        qDebug()<<u8"该聊天窗未打开";
        //1.改变按钮名字
        friendsOfBtn.find(sender)->second->setText(u8"好友" + QString::fromStdString(sender) + "***");
    }
}
/*
 * 添加好友成功！
*/
void MainMeau::addFriendSucessfully(Message m){
    qDebug()<<"addFriendSucessfully:begin";
    //增加按钮
    QPushButton* newFriend = new QPushButton(this);
    QString btnName = m.sender;
    string fid = btnName.left(4).toStdString();
    newFriend->setText(u8"好友" + btnName.left(4));
    newFriend->setFixedSize(200,50);
    ui->Friendlist->addWidget(newFriend);//增加到好友列表页面
    friendsOfBtn.insert(pair<string,QPushButton*>(fid,newFriend));
    //增加好友信息friend
    Firend f;
    strcpy(f.firendId,fid.c_str());
    friends.insert(pair<string,Firend>(fid,f));
    //增加聊天窗口
    Widget* w = new Widget();
    w->sockCli = sockCli;
    w->sender = qq;
    w->recevicer = btnName;
    w->f = &friends.find(fid)->second;
    friendsOfWidget.insert(pair<string,Widget*>(fid,w));  //增加窗口
    //增加槽函数
    connect(newFriend,&QPushButton::clicked,this,[=](){
        Widget* thisWidget = friendsOfWidget.find(fid)->second;
        newFriend->setText(u8"好友" + QString::fromStdString(fid));
        if(thisWidget->isOpen == false){
            thisWidget->setWindowTitle(u8"好友" + btnName.left(4));
            thisWidget->ui->ShowText->setText(thisWidget->f->chatContent);
            thisWidget->show();
            thisWidget->isOpen = true;
        }
    });
}
void MainMeau::closeEvent(QCloseEvent *event){
    m.messageType = 1;//通知服务器
    strcpy(m.sender,qq.toStdString().c_str());
    qDebug()<<send(sockCli, (char*)&m,sizeof(m), 0);//发送
    qDebug()<<"closeEvent";
    closesocket(sockCli);//关闭套接字
    //QThreadOfConnectThread->exit();
    exit(0);
}
/*
 * 添加好友按钮
*/
void MainMeau::on_addFriendButton_clicked()
{
    AddFriendWidget* afw = new AddFriendWidget(qq,&friends);
    afw->setWindowTitle(u8"添加好友");
    afw->show();
}
/*
 * 查看好友申请
*/
void MainMeau::on_newFriend_clicked()
{
    AddFriendListWidget* adlw = new AddFriendListWidget(&newFriend,sockCli,qq.toStdString());
    adlw->setWindowTitle(u8"新朋友");
    adlw->show();
}
MainMeau::~MainMeau()
{
    qDebug()<<u8"MainMeau析构:";
    delete ui;
}
